package pri.damai.xiaowu.auth.manager;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.stereotype.Component;
import pri.damai.xiaowu.auth.security.loginlogic.LoginTypeEnums;
import pri.damai.xiaowu.auth.security.model.UserInfo;
import pri.damai.xiaowu.common.core.enums.ResultCommonEnum;
import pri.damai.xiaowu.common.core.vo.ResultVO;
import pri.damai.xiaowu.common.security.model.AuthRole;
import pri.damai.xiaowu.usercenter.client.UserCenterDubboClient;
import pri.damai.xiaowu.usercenter.common.dto.LoginDTO;
import pri.damai.xiaowu.usercenter.common.dto.RoleDTO;
import pri.damai.xiaowu.usercenter.common.dto.UserDTO;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 与用户服务通信
 *
 * @Desc
 * @Author DaMai
 * @Date 2021/10/18 15:19
 * 但行好事，莫问前程。
 */
@Slf4j
@Component
public class UserManager {

    @DubboReference(check = false)
    UserCenterDubboClient userCenterDubboClient;

    public UserInfo getUser(String phone) {
        return getUserByPhone(phone);
    }



    /**
     * 根据手机号获取用户
     *
     * @param phone 手机号
     * @return com.ruiruan.ruiruanpreauth.security.model.UserInfo
     * @apiNote [附加描述]
     * @author DaMai
     * @date 2021/11/9 9:52
     */
    public UserInfo getUserByPhone(String phone) {

        LoginDTO loginDTO = new LoginDTO();

        loginDTO.setLoginType(LoginTypeEnums.PHONE_CODE.type())
                .setPhone(phone);

        UserInfo userInfo = callUserServer(loginDTO);

        return userInfo;
    }

    /**
     * 根据证书中用户号获取用户
     *
     * @param caUserNo 证书用户号
     * @return com.ruiruan.ruiruanpreauth.security.model.UserInfo
     * @apiNote [附加描述]
     * @author DaMai
     * @date 2021/11/9 9:52
     */
    public UserInfo getUserByCa(String caUserNo) {

//        LoginDTO loginDTO = this.buildLoginParam(LoginTypeEnums.CA, caUserNo);
//
//        return this.callUserServer(loginDTO);
        return null;
    }

    /**
     * 构建 LoginDTO
     *
     * @param loginType    登录类型
     * @param userIdentify 用户标识
     * @return com.ruiruan.common.dto.LoginDTO
     * @apiNote [附加描述]
     * @author DaMai
     * @date 2021/11/9 9:48
     */
//    private LoginDTO buildLoginParam(LoginTypeEnums loginType, String userIdentify) {
//        LoginDTO loginDTO = new LoginDTO();
//        loginDTO.setNameOrPhoneOrCa(userIdentify);
//        loginDTO.setLogType(loginType.type());
//        return loginDTO;
//    }

    /**
     * 请求用户服务
     *
     * @param loginDTO
     * @return com.ruiruan.ruiruanpreauth.security.model.UserInfo
     * @apiNote [附加描述]
     * @author DaMai
     * @date 2021/11/9 9:46
     */
    private UserInfo callUserServer(LoginDTO loginDTO) {

        ResultVO<UserDTO> resultVO = userCenterDubboClient.getUser(loginDTO);

        if (!ResultCommonEnum.SUCCESS.code().equals(resultVO.getCode())) {
            throw new AuthenticationServiceException(resultVO.getMsg());
        }

        return this.covertToUserInfo(resultVO.getData());
    }

    /**
     * 转换为 UserInfo
     *
     * @param userDTO
     * @return com.ruiruan.ruiruanpreauth.security.model.UserInfo
     * @apiNote [附加描述]
     * @author DaMai
     * @date 2021/11/9 9:45
     */
    private UserInfo covertToUserInfo(UserDTO userDTO) {

        List<AuthRole> roleList = userDTO.getRoleList()
                .stream()
                .map(this::covertToAuthRole)
                .collect(Collectors.toList());

//        OrgDTO orgDTO = userDTO.getOrgList()
//                .stream()
//                .filter(OrgDTO::isCurrent)
//                .findFirst()
//                .orElse(new OrgDTO());

        UserInfo userInfo = new UserInfo();
        userInfo.setId(String.valueOf(userDTO.getUserId()))
                .setPhone(userDTO.getPhone())
                .setAvatarUrl(userDTO.getAvatarUrl())
                .setUsername(userDTO.getUsername())
                .setPhone(userDTO.getPhone())
//                .setOrgId(String.valueOf(orgDTO.getId()))
//                .setOrgName(orgDTO.getName())
                .setRoleList(roleList);
        return userInfo;
    }

    /**
     * 转换为 AuthRole
     *
     * @param roleDTO
     * @return com.ruiruan.security.model.AuthRole
     * @apiNote [附加描述]
     * @author DaMai
     * @date 2021/11/9 9:35
     */
    private AuthRole covertToAuthRole(RoleDTO roleDTO) {
        AuthRole authRole = new AuthRole();
        authRole.setId(String.valueOf(roleDTO.getId()))
                .setName(roleDTO.getName());
        return authRole;
    }

}
